﻿///
/// Developer: Jirapong Nanta (jirapong.nanta@gmail.com)
/// Date: 27 July 2008
/// Copyright @ 2008 www.awsstudio.net
///
using System;
using System.Collections.Generic;
using System.Text;
using System.AWSServices.S3.S3SoapProxy;
using System.AWSServices.Shared;

namespace System.AWSServices.S3
{
    public class Bucket
    {
        #region Properties
        public string Name { get; set; }
        public DateTime CreationDate { get; set; }
        #endregion

        public static void Create(S3Client s3Client, string name)
        {
            if (!s3Client.IsConnected)
                throw new Exception("S3 is not connect yet!, call method S3Client.EstablishConnection().");

            DateTime timestamp = AWSSecurity.TimeStamp;
            string signature = AWSSecurity.CreateSignature(s3Client.SecretKey, S3Client.SERVICE_NAME, "CreateBucket", timestamp);

            //TODO: default to all users and owner
            Grant[] grants = new Grant[1];
            Group groupGrant = new Group();
            groupGrant.URI = "http://acs.amazonaws.com/groups/global/AllUsers";
            grants[0] = new Grant();
            grants[0].Grantee = groupGrant;
            grants[0].Permission = Permission.READ;

            CreateBucketResult result = s3Client.AmazonS3Soap.CreateBucket(name, grants, s3Client.AccessKey, timestamp, true, signature);

            // TODO: set dirty to Bucket
            s3Client.IsDirty = true;
        }
    }
}
